<?php


namespace app\common\controller;


use app\admin\service\RbacService;
use app\common\enum\MainEnum;
use app\common\exception\ForbiddenException;
use app\common\exception\LogicException;
use app\common\exception\NotFoundException;
use app\common\exception\ParameterFailedException;
use app\common\middleware\AuthToken;
use app\common\model\MediaModel;
use app\common\model\OrderListRefundModel;
use app\common\model\OrderModel;
use app\common\model\UserModel;
use app\common\service\Lang;
use app\common\service\Redis;
use app\common\validate\CommonValidate;
use app\common\validate\UsersValidate;
use think\facade\Config;
use think\Request;

class CommonController
{
    protected $middleware
        = [
            AuthToken::class,
        ];
    protected $limit, $page;

    public function __construct()
    {
        $params = self::getParam([
            'page',
            'limit',
        ]);
        (new CommonValidate())->goCheck('paginate', $params);
        $this->limit = $params['limit'] ?? 10;
        $this->page  = $params['page'] ?? 1;
        //--$this->page;//tp6 limit方法特性，page从0开始
    }

    protected static function getParam($params)
    {
        $headers      = \request()->header();
        $clientParams = (array)json_decode(urldecode(\request()->header('client-params', '')), true);
        $clientParams = array_merge($headers, $clientParams);
        //        vd($clientParams);
        if (is_array($params)) {
            $return = [];
            foreach ($params as $item) {
                $return[$item] = self::filterParam($clientParams[$item] ?? null);
            }
            return $return;
        }

        return self::filterParam($request[strtolower($params)] ?? null);
    }

    private static function filterParam($param)
    {
        if (is_array($param)) {
            foreach ($param as &$item) {
                $item = self::filterParam($item);
            }
            return $param;
        }
        if (is_bool($param)) {
            return $param;
        }
        # 核心过滤
        return trim($param);
    }

    protected static function getWhere($array = []): array
    {
        $where = [];
        foreach ($array as $k => $v) {
            if ($v) {
                if (strpos($k, '_time') !== false) {
                    $where[] = [
                        $k,
                        'between',
                        $v,
                    ];
                    continue;
                }

                if (strpos($v, '%') !== false) {
                    $where[] = [
                        $k,
                        'like',
                        $v,
                    ];
                    continue;

                }
                $where[] = [
                    $k,
                    '=',
                    $v,
                ];
                continue;

            }
        }
        return $where;

    }

    public static function arrangeProduct(&$modelArr, $many = false): void
    {
        if ($many) {
            foreach ($modelArr as &$v) {
                self::arrangeProductSku($v);
            }
        }
        else {
            self::arrangeProductSku($modelArr);
        }
    }

    public static function arrangeProductSku(&$modelArr)
    {
        if (isset($modelArr['skuInfo'])) {
            //最大值最小值
            $prices     = [];
            $costPrices = [];
            $sku_stock  = 0;
            $skus       = [];
            foreach ($modelArr['skuInfo'] as &$item) {
                $trueStock                    = $item['stock'] - $item['virtual_sales'] - $item['sales'];
                $prices []                    = $item['price'];
                $costPrices []                = $item['cost_price'];
                $sku_stock                    += $trueStock < 0 ? 0 : $trueStock;
                $item['extend_surplus_stock'] = $trueStock < 0 ? 0 : $trueStock;
                $skus []                      = $item['sku_name'];
            }
            $prices     = array_unique($prices);
            $costPrices = array_unique($costPrices);
            switch (count($prices)) {
                case 0:
                    $modelArr['extend_min_max_price'] = null;
                    $modelArr['extend_min_price']     = null;
                    break;
                case 1:
                    $modelArr['extend_min_max_price'] = (float)($prices[0]);
                    $modelArr['extend_min_price']     = (float)($prices[0]);
                    break;
                default:
                    $modelArr['extend_min_max_price'] = (float)min($prices) . '~' . (float)max($prices);
                    $modelArr['extend_min_price']     = (float)min($prices);
            }
            switch (count($costPrices)) {
                case 0:
                    $modelArr['extend_cost_price'] = null;
                    break;
                case 1:
                    $modelArr['extend_cost_price'] = (float)($costPrices[0]);
                    break;
                default:
                    $modelArr['extend_cost_price'] = (float)min($costPrices) . '~' . (float)max($costPrices);
            }

            $modelArr['extend_sku_names']     = implode('/', $skus);
            $modelArr['extend_surplus_stock'] = $sku_stock;
        }
    }
}